package com.yazi.mxz.behavior.controller;

import java.util.Iterator;

import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.Store;
import com.yazi.mxz.behavior.jackson.filter.StoreFilter;
import com.yazi.mxz.behavior.repository.StoreRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("storeController")
@RequestMapping("/store")
public class StoreController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreController.class);

	@Resource
	StoreRepository storeRepository;

	/**
	 * 查询(lng,lat)或者id附近的店铺,其中(lng,lat)和id至少要满足一项
	 * 
	 * @param w
	 *            searchWord
	 * @param x
	 *            lng
	 * @param y
	 *            lat
	 * @param i
	 *            regionId
	 * @param d
	 *            distance
	 * @return
	 */
	@RequestMapping("/near/json")
	@ResponseBody
	public ObjectResult<?> nearJson(String w, Double x, Double y, Long i, double d,
			Pageable pageable) {
		StringBuffer sort = new StringBuffer();
		if (null != pageable && null != pageable.getSort()) {
			Iterator<Order> it = pageable.getSort().iterator();
			Order order;
			while (it.hasNext()) {
				order = it.next();
				sort.append("," + order.getProperty() + "," + order.getDirection().name());
			}
		}

		log.debug("nearJson: {},{},{},{}", new Object[] { x, y, d, sort.toString() });

		ObjectResult<Page<Store>> jsonResult = new ObjectResult<Page<Store>>();
		jsonResult.setFilter(StoreFilter.get());
		try {

			Distance dis = new Distance(d, Metrics.KILOMETERS);
			
			// 默认在南宁地区
			Point point = new Point(108.3, 22.8);
			if (null != x && null != y) {
				point = new Point(x, y);
			}

			Page<Store> page;
			if (null != w && !w.isEmpty()) {
				page = storeRepository.findByNameLikeAndLocationNear(w, point, dis, pageable);
			} else {
				page = storeRepository.findByLocationNear(point, dis, pageable);
			}

			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/oneById/json")
	@ResponseBody
	public ObjectResult<?> oneByIdJson(long storeId) {		
		log.debug("oneByIdJson: {}", storeId);

		ObjectResult<Store> jsonResult = new ObjectResult<Store>();
		jsonResult.setFilter(StoreFilter.get());
		try {
			jsonResult.setResult(storeRepository.findOne(storeId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}

}
